package your.packag;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import DAO.MyData;
import DTO.DepositDTO;
import DTO.WithdrawalDTO;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class WithdrawalActivity extends Activity implements OnClickListener{

	Spinner accSpinner;
	EditText money;
	DefaultHttpClient postClient;
	WithdrawalDTO withdrawalDTO;
	String valueacc;
	Button create;
	ProgressDialog dialog;
	List<Cookie> cookies;
	private Handler handler = new Handler() 
	{
		@Override
	    public void handleMessage(Message msg) 
		{
			dialog.dismiss();
	        LoadUI();
	        	

	    }
	};
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.withdrawal);
		postClient = LoginActivity.postClient;
		dialog = new ProgressDialog(this);
        dialog = ProgressDialog.show(this, "", 
                "Loading. Please wait...", true);
        new Thread(new Runnable() {
			
			  
				  public void run() {
					  LoadData();
					  handler.sendEmptyMessage(0);
				  }}).start();
	}
	
	public void LoadUI()
	{
		
		accSpinner = (Spinner)findViewById(R.id.withdrawalspinner1);	
		 money = (EditText)findViewById(R.id.withdrawaledittext1);
		 create = (Button)findViewById(R.id.withdrawalbutton1);
		 ArrayAdapter<String> account = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, withdrawalDTO.account());
			account.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			accSpinner.setAdapter(account);
			
			create.setOnClickListener(this);
			

			
	}
	
	public void LoadData() 
	{
		
        
        String xml;
		try {
			/*HttpGet httpGet = new HttpGet("http://smartwebvina.com/smartlive/index.php?option=com_mymonney");
	        HttpResponse response = LoginActivity.postClient.execute(httpGet);
	        HttpEntity httpEntity =response.getEntity();
			xml = EntityUtils.toString(httpEntity);
			org.jsoup.nodes.Document doc = Jsoup.parse(xml);*/
			MyData data = new MyData();
			withdrawalDTO = data.readWithdrawal(ManagementActivity.doc);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} //catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		//}
		
		
	
	}
	public List<Cookie> Withdrawal(String url,String money,String valueacc) {

		HttpPost httpPost = new HttpPost(url);

		   HttpResponse response;
		   List<Cookie> cookies = null;
		  
		

		   try {   
			  
		      // Make a List. Increase the size as you wish.
		      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

		      // Add your form name and a text that belongs to the actual form.
		      
		     
		      nameValuePairs.add(new BasicNameValuePair("balance",money));
		      nameValuePairs.add(new BasicNameValuePair("danhmuc",valueacc));
		   
		
		    //  nameValuePairs.add(new BasicNameValuePair("remember", "yes"));
		      

		      nameValuePairs.add(new BasicNameValuePair("option", "com_mymonney")); 
		      nameValuePairs.add(new BasicNameValuePair("id", "2"));  
		      nameValuePairs.add(new BasicNameValuePair("currency", "VND"));
		      nameValuePairs.add(new BasicNameValuePair("task", "withdrawal"));
		      nameValuePairs.add(new BasicNameValuePair("view", "parents"));
		     nameValuePairs.add(new BasicNameValuePair("f6f6cb580925b9a2a5f5d2e83482456e", "1"));

		      // Set the entity of your HttpPost.
		      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

		      // Execute your request against the given url and catch the response.
		      response = postClient.execute(httpPost);
		    
		      BufferedReader rd = new  BufferedReader(new  InputStreamReader(
		    		  response.getEntity().getContent()));

		    		 String line = "";
		    		  while  ((line = rd.readLine()) != null) {
		    		  System.out.println(line);
		    		  }
		      // Status code 200 == successfully posted data.
		      if(response.getStatusLine().getStatusCode() == 200) {
		         // Green light. Catch your cookies from your HTTP response.
		          cookies =  postClient.getCookieStore().getCookies(); 
		         return  cookies;
		      }
		   } catch (Exception e) {
		   }
		   return  cookies;
	}

	public void getValueSpinner()
	{
		String temp = accSpinner.getSelectedItem().toString();
		for(int i =0 ;i< withdrawalDTO.account().size(); i++)
		{
			if(temp.compareTo(withdrawalDTO.account().get(i))==0)
			{
				valueacc = withdrawalDTO.valueacc().get(i);
			}
		}
	}
	
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId())
		{
			case R.id.withdrawalbutton1:
				getValueSpinner();
				cookies = Withdrawal("http://smartwebvina.com/smartlive/index.php?option=com_mymonney",		
						money.getText().toString(),
					valueacc);
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setMessage("Create Sucess")
			       .setCancelable(false)
			       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			               dialog.cancel();
			           }
			       });
				AlertDialog alert = builder.create();
				alert.show();
				break;
		}
		
	}



}
